import { addOrEditRes, deleteTradeRes, getTradeListRes } from "@/api/product/trademark";


const state=()=>{
    return{
        tradeList:[],
        total:0
    }
}

const mutations={
    GETTRADE(state,data){
        state.tradeList  = data.records;
        state.total = data.total;
    }
};

const actions={
    async getTradeList({commit,state,getters,dispatch},{page,limit}){
        try {
            let result = await getTradeListRes(page,limit);
            commit("GETTRADE",result.data);
            if(!result.data.records.length){
                dispatch("getTradeList",{page:page-1,limit});
            }
            return "ok"
        } catch (error) {
            return Promise.reject("获取失败")
        }
    },
    async addOrEditTrade({commit,state,dispatch,getters},data){
        try {
            await addOrEditRes(data);
            return "ok"
        } catch (error) {
            return Promise.reject("新增品牌失败")
        }
    },
    async deleteTrade({commit,dispatch,state,getters},id){
        try{
            await deleteTradeRes(id);

            return "ok"
        }catch(err){

            return Promise.reject("删除品牌失败失败")
        }
    }
};

const getters={};


export default {
    state,
    mutations,
    actions,
    getters
}